package com.shang.controller;

import com.shang.pojo.Items;
import com.shang.pojo.ItemsImg;
import com.shang.pojo.ItemsParam;
import com.shang.pojo.ItemsSpec;
import com.shang.pojo.vo.CommentLevelCountsVO;
import com.shang.pojo.vo.ItemInfoVO;
import com.shang.pojo.vo.ShopCartVO;
import com.shang.service.ItemService;
import com.shang.utils.PagedGridResult;
import com.shang.utils.ShangJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商品接口",tags = {"商品信息相关"})
@RestController
@RequestMapping("items")
public class ItemsController extends BaseController {

    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "查询商品详情",notes = "",httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public ShangJSONResult info(@PathVariable String itemId){
        if (StringUtils.isBlank(itemId)){
            return ShangJSONResult.errorMsg(null);
        }
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return ShangJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品评价等级",notes = "",httpMethod = "GET")
    @GetMapping("/commentLevel")
    public ShangJSONResult commentLevel(@RequestParam String itemId){
        if (StringUtils.isBlank(itemId)){
            return ShangJSONResult.errorMsg(null);
        }
        CommentLevelCountsVO counts = itemService.queryCommentCounts(itemId);
        return ShangJSONResult.ok(counts);
    }

    @ApiOperation(value = "查询商品评论",notes = "",httpMethod = "GET")
    @GetMapping("/comments")
    public ShangJSONResult comments(@RequestParam String itemId,
                                    @RequestParam Integer level,
                                    @RequestParam Integer page,
                                    @RequestParam Integer pageSize){
        if (StringUtils.isBlank(itemId)){
            return ShangJSONResult.errorMsg(null);
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult grid = itemService.queryPagedComments(itemId, level, page, pageSize);
        return ShangJSONResult.ok(grid);
    }

    @ApiOperation(value = "搜索商品",notes = "",httpMethod = "GET")
    @GetMapping("/search")
    public ShangJSONResult search(@RequestParam String keywords,
                                    @RequestParam String sort,
                                    @RequestParam Integer page,
                                    @RequestParam Integer pageSize){
        if (StringUtils.isBlank(keywords)){
            return ShangJSONResult.errorMsg(null);
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }
        PagedGridResult grid = itemService.searchItems(keywords, sort, page, pageSize);
        return ShangJSONResult.ok(grid);
    }

    @ApiOperation(value = "分类id搜索商品",notes = "",httpMethod = "GET")
    @GetMapping("/catItems")
    public ShangJSONResult catItems(@RequestParam Integer catId,
                                  @RequestParam String sort,
                                  @RequestParam Integer page,
                                  @RequestParam Integer pageSize){
        if (catId == null){
            return ShangJSONResult.errorMsg(null);
        }
        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }
        PagedGridResult grid = itemService.searchItemsByThirdCat(catId, sort, page, pageSize);
        return ShangJSONResult.ok(grid);
    }


    @ApiOperation(value = "购物车",notes = "",httpMethod = "GET")
    @GetMapping("/refresh")
    public ShangJSONResult refresh(@RequestParam String itemSpecIds){
        if (StringUtils.isBlank(itemSpecIds)){
            return ShangJSONResult.ok();
        }

        List<ShopCartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
        return ShangJSONResult.ok(list);
    }
}
